package Thread;

public class ThreadDemo15 {
    public static void main(String[] args) {

        Object A = new Object();
        Object B = new Object();

        Thread t1 = new Thread(() -> {
            synchronized (A) {
                try {
                    // 给t2一些时间，让t2能拿到锁B
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 在不释放A锁的情况下，尝试去拿B锁
                synchronized (B) {
                    System.out.println("t1拿到了B锁");
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized (B) {
                try {
                    // 给t1一些时间，让t1能拿到锁A
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 在不释放B锁的情况下，去尝试拿A锁
                synchronized (A) {
                    System.out.println("t2拿到了A锁");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
